package goods

import (
	"192.168.1.75/go-pkg/helper"
	"192.168.1.75/go-pkg/logx"
	"context"
	"gorm.io/gorm"
	"sale-admin/internal/dao"
	"sale-admin/internal/models"
	"strconv"
)

type GoodsLogService struct{}

func (t GoodsLogService) List(ctx context.Context, goodId string, page *helper.PageInfo) ([]*models.MaGoodsInfoLogJoinType, int64, error) {
	goodsLogRepo := dao.NewGoodsLogRepo(ctx)
	list, size, err := goodsLogRepo.ListByFilterV2(ctx, &dao.GoodsLogFilterJoin{GoodsID: goodId}, page)
	if err != nil {
		logx.Error(ctx, "ListByFilter err: %v", logx.Any("err", err))
		return nil, 0, err
	}

	return list, size, nil
}

func (t GoodsLogService) InsertLog(ctx context.Context, tx *gorm.DB,
	merchantId string, old *models.MaGoodsInfo, new *models.MaGoodsInfo) (err error) {
	if old.GoodsID != new.GoodsID {
		logx.Error(ctx, "goodsInfo id error", logx.Any("old", old), logx.Any("new", new))
		//这个继续执行，防止记漏了，但正确调用时，不会出现id不一致
	}
	goodsLogRepo := dao.NewGoodsLogRepo(tx)
	// 以下几个值变化时，记录变更，记录以下字段新旧值
	// 商品名称、商品价格、运费模板、评测奖励、商品编码、抵扣上限
	if old.GoodsName1 != new.GoodsName1 {
		err = goodsLogRepo.Insert(ctx, &models.MaGoodsInfoLog{
			GoodsID:     new.GoodsID,
			MerchantID:  merchantId,
			FieldName:   "GoodsName1",
			FieldNameCn: "商品名字1",
			OldValue:    old.GoodsName1,
			NewValue:    new.GoodsName1,
		})
		if err != nil {
			logx.Error(ctx, "insert log error", logx.Any("err", err))
			return err
		}
	}

	if old.GoodsName2 != new.GoodsName2 {
		err = goodsLogRepo.Insert(ctx, &models.MaGoodsInfoLog{
			GoodsID:     new.GoodsID,
			MerchantID:  merchantId,
			FieldName:   "GoodsName2",
			FieldNameCn: "商品名字2",
			OldValue:    old.GoodsName2,
			NewValue:    new.GoodsName2,
		})
		if err != nil {
			logx.Error(ctx, "insert log error", logx.Any("err", err))
			return err
		}
	}

	if old.Price != new.Price {
		err = goodsLogRepo.Insert(ctx, &models.MaGoodsInfoLog{
			GoodsID:     new.GoodsID,
			MerchantID:  merchantId,
			FieldName:   "Price",
			FieldNameCn: "商品价格",
			OldValue:    strconv.FormatFloat(old.Price, 'f', -1, 64),
			NewValue:    strconv.FormatFloat(new.Price, 'f', -1, 64),
		})
		if err != nil {
			logx.Error(ctx, "insert log error", logx.Any("err", err))
			return err
		}
	}

	if old.ExpressFeeTemplate != new.ExpressFeeTemplate {
		err = goodsLogRepo.Insert(ctx, &models.MaGoodsInfoLog{
			GoodsID:     new.GoodsID,
			MerchantID:  merchantId,
			FieldName:   "ExpressFeeTemplate",
			FieldNameCn: "运费模板",
			OldValue:    old.ExpressFeeTemplate,
			NewValue:    new.ExpressFeeTemplate,
		})
		if err != nil {
			logx.Error(ctx, "insert log error", logx.Any("err", err))
			return err
		}
	}

	if old.ReviewPoints != new.ReviewPoints {
		err = goodsLogRepo.Insert(ctx, &models.MaGoodsInfoLog{
			GoodsID:     new.GoodsID,
			MerchantID:  merchantId,
			FieldName:   "ReviewPoints",
			FieldNameCn: "评测奖励积分",
			OldValue:    strconv.Itoa(old.ReviewPoints),
			NewValue:    strconv.Itoa(new.ReviewPoints),
		})
		if err != nil {
			logx.Error(ctx, "insert log error", logx.Any("err", err))
			return err
		}
	}

	if old.ExternalProductID != new.ExternalProductID {
		err = goodsLogRepo.Insert(ctx, &models.MaGoodsInfoLog{
			GoodsID:     new.GoodsID,
			MerchantID:  merchantId,
			FieldName:   "GoodsCode",
			FieldNameCn: "商品编码",
			OldValue:    old.ExternalProductID,
			NewValue:    new.ExternalProductID,
		})
		if err != nil {
			logx.Error(ctx, "insert log error", logx.Any("err", err))
			return err
		}
	}

	if old.DeductionLimit != new.DeductionLimit {
		err = goodsLogRepo.Insert(ctx, &models.MaGoodsInfoLog{
			GoodsID:     new.GoodsID,
			MerchantID:  merchantId,
			FieldName:   "DeductionLimit",
			FieldNameCn: "抵扣上限",
			OldValue:    strconv.FormatFloat(old.DeductionLimit, 'f', -1, 64),
			NewValue:    strconv.FormatFloat(new.DeductionLimit, 'f', -1, 64),
		})
		if err != nil {
			logx.Error(ctx, "insert log error", logx.Any("err", err))
			return err
		}
	}

	return nil
}
